export const state = ()=> ({
  token:'',
  userInfo:{}
})

const getter = () => {}

export const mutations ={
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
  },
}

export const actions = {
  nuxtServerInit({ commit }, { req, app }) {
    // 从 cookies 获取 token 和 userInfo
    const token = app.$cookies.get('token')
    const userInfo = app.$cookies.get('userInfo')

    if (token) {
      commit('SET_TOKEN', token)
    }
    if (userInfo) {
      commit('SET_USERINFO', userInfo)
    }
  },
  // 使用 actions 来访问 $cookies
  setToken({ commit }, token) {
    commit('SET_TOKEN', token)
    this.$cookies.set('token', token)
  },
  getToken({ commit }) {
    const token = this.$cookies.get('token')
    commit('SET_TOKEN', token)
  },
  setUserInfo({ commit }, userInfo) {
    commit('SET_USERINFO', userInfo)
    this.$cookies.set('userInfo', JSON.stringify(userInfo))
  },
  getUserInfo({ commit }) {
    const userInfo = this.$cookies.get('userInfo')
    commit('SET_USERINFO', JSON.parse(userInfo))
  }
};
